﻿//Copyright (c) 2010, Andreas Grünwald
//All rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Security;

namespace yaTDL.Core.MVVM
{
    public class LoginViewModel : ViewModelBase
    {
        #region Fields

        RelayCommand _loginCommand;
        RelayCommand _cancelCommand;

        string _userIdOriginaleValue;
        SecureString _passwordOriginalValue;

        string _userId;
        SecureString _password;

        #endregion

        #region Properties
        
        public ICommand LoginCommand
        {
            get 
            {
                if (_loginCommand == null)
                {
                    _loginCommand = new RelayCommand(p => this.loginCommandExecute(p), p => this.loginCommandCanExecute(p));
 
                }
                return _loginCommand;
            } 
        }

        public ICommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new RelayCommand(p => this.cancelCommandExecute(p));
                }
                return _cancelCommand;
            }
        }


        public string UserId
        {
            get { return _userId; }
            set
            {
                if (value != _userId)
                {
                    _userId = value;
                    RaisePropertyChanged("UserId");
                }
            }
        }

        public SecureString Password
        {
            get { return _password; }
            set
            {
                if (value != _password)
                {
                    _password = value;
                    RaisePropertyChanged("Password");
                }
            }           
        }

        #endregion

        public event EventHandler Completed;


        public LoginViewModel(string userid, SecureString password)
        {
            _userIdOriginaleValue = userid;
            _passwordOriginalValue = password;

            UserId = userid;
            Password = password;
        }


        #region CommandMethods

        private bool loginCommandCanExecute(object parameter)
        {
            if (Password == null) { return false; }
            return !String.IsNullOrEmpty(UserId) && Password.Length > 0 ? true : false;
        }

        private void loginCommandExecute(object parameter)
        {
            CloseView();
        }


        private void cancelCommandExecute(object parameter)
        {
            Password = _passwordOriginalValue;
            UserId = _userIdOriginaleValue;
            OnCompleted();
        }

        #endregion


        private void OnCompleted()
        {
            if (Completed != null)
            { 
                Completed(this, EventArgs.Empty); 
            }
        }
    }
}
